package com.xyz.business.dao.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.xyz.base.dao.BaseDao;
import com.xyz.business.bean.Cover;
import com.xyz.business.bean.Enterprise;
import com.xyz.business.bean.Report;
import com.xyz.business.dao.ReportDao;

/**
 * 
 * @author xyzjh
 * 
 */
public class ReportDaoImpl extends BaseDao implements ReportDao {

	Logger logger = Logger.getLogger(ReportDaoImpl.class);

	/**
	 * @param report
	 *            Report
	 * @param enterprise
	 *            Enterprise
	 * @param cover
	 *            Cover
	 * @throws SQLException
	 *             SQLException
	 * @see com.xyz.business.dao.ReportDao#insert(com.xyz.business.bean.Report,
	 *      com.xyz.business.bean.Enterprise)
	 */
	public void insert(Report report, Enterprise enterprise, Cover cover)
			throws SQLException {
		logger.info(" insert begin...");
		Date now = new Date();
		enterprise.setInsertdate(now);
		enterprise.setUpdatetime(now);
		report.setInsertdate(now);
		report.setUpdatetime(now);
		cover.setInsertdate(now);
		cover.setUpdatetime(now);
		sqlMapClient.startTransaction(); // 事务开始
		try {
			sqlMapClient.insert("insertEnterprise", enterprise);
			sqlMapClient.insert("insertReport", report);
			sqlMapClient.insert("insertCover", cover);
			sqlMapClient.commitTransaction(); // 提交事务，从开始到这里是一个事务
		} finally {
			sqlMapClient.endTransaction(); // 事务结束
		}
		logger.info(" insert end...");
	}

	@Override
	public List query(Report report, Enterprise enterprise, int pageNow,
			int pageSize) throws SQLException {
		logger.info(" query begin...");
		// TODO Auto-generated method stub
		StringBuffer andSql = new StringBuffer(32);

		String selectSql = "select r.*,e.* from t_report r,t_enterprise e";
		// ishide表示显示
		String whereSql = " where r.eid=e.eid and r.ishide=0 ";
		andSql.append(selectSql).append(whereSql);
		rtnWhereSql(report, enterprise, andSql);
		andSql.append(" order by r.updatetime desc ").append(" limit ")
				.append(pageNow).append(",").append(pageSize);
		logger.info(" sql [" + andSql + "]");
		logger.info(" query end...");
		return sqlMapClient.queryForList("queryReportList", andSql.toString());
	}

	public int queryReportCount(Report report, Enterprise enterprise)
			throws SQLException {
		logger.info(" queryReportCount begin...");
		StringBuffer andSql = new StringBuffer(32);
		String countSql = "SELECT count(r.eid) from t_report r,t_enterprise e";
		String whereSql = " where r.eid=e.eid and r.ishide=0 ";
		andSql.append(countSql).append(whereSql);
		logger.info(" sql [" + andSql + "]");
		logger.info(" queryReportCount end...");
		return (Integer) sqlMapClient.queryForObject("queryReportCount",
				andSql.toString());
	}

	private void rtnWhereSql(Report report, Enterprise enterprise,
			StringBuffer andSql) {

		if (report == null || enterprise == null) {
			return;
		}
		if (!StringUtils.isBlank(report.getReportyear())) {
			andSql.append(" and r.reportyear='").append(report.getReportyear())
					.append("'");
		}
		if (null != report.getReporttype() && report.getReporttype() > 0) {
			andSql.append(" and r.reporttype=").append(report.getReporttype());
		}
		if (!StringUtils.isBlank(report.getRid())) {
			andSql.append(" and r.rid='").append(report.getRid()).append("'");
		}
		if (!StringUtils.isBlank(report.getReportno())) {
			andSql.append(" and r.reportno='").append(report.getReportno())
					.append("'");
		}
		// -1表示没有选择具体的项目负责人
		if (!StringUtils.isBlank(report.getProjectleader())
				&& !"-1".equals(report.getProjectleader())) {
			andSql.append(" and r.projectleader ='")
					.append(report.getProjectleader()).append("'");
		}

		if (!StringUtils.isBlank(enterprise.getCompanyname())) {
			andSql.append(" and e.companyname like '%")
					.append(enterprise.getCompanyname()).append("%'");
		}
		if (null != report.getReceiptdate()) {
			andSql.append(" and r.receipdate='")
					.append(report.getReceiptdate()).append("'");
		}
		if (null != report.getSenddate()) {
			andSql.append(" and r.senddate='").append(report.getSenddate())
					.append("'");
		}
		if (null != report.getFinishdate()) {
			andSql.append(" and r.finishdate='").append(report.getFinishdate())
					.append("'");
		}
	}

	public Report queryReport(String rptId) throws SQLException {
		logger.info(" queryReport begin...");

		String sql = "select * from t_report where rid='" + rptId
				+ "'  and ishide=0 ";
		Report report = (Report) sqlMapClient
				.queryForObject("queryReport", sql);
		logger.info(" sql [" + sql + "]");
		logger.info(" queryReport end...");
		return report;
	}

	@Override
	public void deleteReport(String rptid) throws SQLException {
		Report report = queryReport(rptid);
		if (report != null) {
			sqlMapClient.startTransaction(); // 事务开始
			try {
				sqlMapClient.delete("deleteEnterprise", report.getEid());
				sqlMapClient.delete("deleteReport", rptid);
				sqlMapClient.commitTransaction(); // 提交事务，从开始到这里是一个事务
			} finally {
				sqlMapClient.endTransaction(); // 事务结束
			}
		}

	}

	@Override
	public void editReport(String rptid) throws SQLException {
		sqlMapClient.update("editReport");

	}

	@Override
	public Report queryReportByRptId(String rid) throws SQLException {
		// TODO Auto-generated method stub
		return (Report) sqlMapClient.queryForObject("queryReportByRptId", rid);
	}

	@Override
	public void update(Report report, Enterprise enterprise, Cover cover)
			throws SQLException {
		logger.info(" update begin...");
		Date now = new Date();
		enterprise.setUpdatetime(now);
		report.setUpdatetime(now);
		cover.setUpdatetime(now);
		sqlMapClient.startTransaction(); // 事务开始
		try {
			sqlMapClient.update("updateEnterprise", enterprise);
			sqlMapClient.update("updateReport", report);
			sqlMapClient.update("updateCover", cover);
			sqlMapClient.commitTransaction(); // 提交事务，从开始到这里是一个事务
		} finally {
			sqlMapClient.endTransaction(); // 事务结束
		}
		logger.info(" update end...");

	}

	@Override
	public void hide(String rptid) throws SQLException {
		sqlMapClient.update("hideReport", rptid);
	}

	@Override
	public List queryCollect(Report report, Enterprise enterprise,
			Date beginTime, Date endTime) throws SQLException {
		String sql = "select r.*,e.* from t_report r,t_enterprise e  where r.eid=e.eid and r.ishide=0 ";
		StringBuilder sBuilder = new StringBuilder(32);
		sBuilder.append(sql);
		if (!StringUtils.isBlank(report.getReportyear())) {
			sBuilder.append(" and r.reportyear='")
					.append(report.getReportyear()).append("'");
		}
		if (null != beginTime) {
			sBuilder.append(" and r.finishdate >= '").append(beginTime)
					.append("'");
		}
		if (null != endTime) {
			sBuilder.append(" and r.finishdate <= '").append(endTime)
					.append("'");
		}
		return sqlMapClient
				.queryForList("queryReportList", sBuilder.toString());
	}

}
